/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: Question.java
 * Date: 13/11/2010
 */

package com.ingenius.sau.shared.model.instruments;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.annotations.Element;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.FetchGroup;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.ingenius.sau.shared.model.core.BaseBean;

/**
 * Class <strong>Question</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
@FetchGroup(name = "with_options", members = { @Persistent(name = "responseOptions") })
public class Question implements BaseBean, Instrument {

	// Constants
	public static final String TYPE_OPEN_RESPONSE = "Open Response";

	public static final String TYPE_ONLY_RESPONSE = "Only Response";

	public static final String TYPE_MULTIPLE_RESPONSE = "Multiple Response";

	// DATA
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
	private String id;

	@Persistent
	private String title;

	@Persistent
	private String content;

	@Persistent
	private String type;

	@Persistent(mappedBy = "question")
	@Element(dependent = "true")
	private List<ResponseOption> responseOptions;

	@NotPersistent
	private String options;

	// CONSTRUCTORS

	public Question() {
	}

	public Question(String id) {
		this.id = id;
	}

	// METHODS

	public String getId() {
		return id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		if (TYPE_MULTIPLE_RESPONSE.equals(type)
				|| TYPE_ONLY_RESPONSE.equals(type)
				|| TYPE_OPEN_RESPONSE.equals(type)) {
			this.type = type;

		} else {
			System.out.println("Invalid argument exception. [" + type + "]");
		}
	}

	public List<ResponseOption> getResponses() {
		return (responseOptions == null) ? responseOptions = new ArrayList<ResponseOption>()
				: responseOptions;
	}

	public void setResponses(List<ResponseOption> responseOptions) {
		this.responseOptions = responseOptions;
	}

	public void addResponseOption(ResponseOption responseOption) {
		getResponses().add(responseOption);
	}

	public String getOptions() {
		options = responseOptions.toString();
		return options.substring(1, options.length() - 1);
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Question) {
			return ((Question) obj).getId().equals(id);
		}
		return false;
	}

	@Override
	public String toString() {
		return title + " - " + content;
	}

	public Question toDTO() {
		System.out.println(options);
		return this;
	}

}
